//
//  DiseaseOnSetController.m
//  KYProject
//
//  Created by Roman on 16/12/23.
//  Copyright © 2016年 Roman. All rights reserved.
//

#import "DiseaseOnSetController.h"
#import "NetWorkTool.h"
#import "DiseaseOnSetModel.h"
#import "DiseaseOnSetCell.h"

@interface DiseaseOnSetController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic ,weak) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *illDataArray;

@end

static NSString*cellID = @"DiseaseOnSetCell";
@implementation DiseaseOnSetController


#pragma mark
#pragma mark - 懒加载
- (NSMutableArray *)illDataArray
{
    if (!_illDataArray) {
        _illDataArray = [NSMutableArray array];
    }
    return _illDataArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"并发症";
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(clickRightBtn:)];
    
    rightBtn.tintColor = [UIColor blackColor];
    
    self.navigationItem.rightBarButtonItem = rightBtn;
    
    //创建tableVIew
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [self.view addSubview:tableView];
    tableView.rowHeight = 50;
    tableView.dataSource = self;
    tableView.delegate = self;
    //注册cell
     [tableView registerNib:[UINib nibWithNibName:@"DiseaseOnSetCell" bundle:nil] forCellReuseIdentifier:cellID];
        self.tableView = tableView;
    //获取数据
    [self dataWithDiseaseType];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DiseaseOnSetCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
    DiseaseOnSetModel *model = self.dataArray[indexPath.row];
    
    //cell.textLabel.text = model.complication_name;
    cell.lbl.text = model.complication_name;
  
   cell.arr = self.illDataArray;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DiseaseOnSetCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    //在点击cell中掉cell.点击btn的方法,使点击cell相当于点击btn的效果.
    [cell clickBtn:cell.btn];
    [cell setSelected:NO animated:NO];
}

//点击右上角的btn
-(void)clickRightBtn:(UIBarButtonItem *)sender{
    NSInteger rowNum = [_tableView numberOfRowsInSection:0];
    for (int i = 0; i < rowNum; i++) {
        NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:0];
        DiseaseOnSetCell *cell = [_tableView cellForRowAtIndexPath:indexpath];
        
        
        if (cell.illName&&![self.illDataArray containsObject:cell.illName]) {
            [self.illDataArray addObject:cell.illName];
        }
        
    }
    
    //    NSLog(@"%@",self.illArray);
    
    //发通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"DiseaseOnSet" object:self.illDataArray userInfo:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark
#pragma mark - 网络加载数据
- (void)dataWithDiseaseType
{
    
    __weak DiseaseOnSetController *weak = self;
    NSString *str = @"http://iosapi.itcast.cn/complicationList.json.php";
    
    NSDictionary *parmasDic = @{@"page":@(self.dataNumber),@"page_size":@(15),@"ci2_id":@(3)};
    
    NSLog(@"%ld",(long)self.dataNumber);
   
    [[NetWorkTool shareManager] POST_RequestWithUrlString:str parmas:parmasDic successBloock:^(id responseObject) {
        
        NSArray *arry = responseObject[@"data"];
        
        weak.dataArray = [NSArray yy_modelArrayWithClass:[DiseaseOnSetModel class] json:arry];
        [weak.tableView reloadData];
        
    } failureBlock:^(NSError *error) {
        
    }];
    
    

}



@end
